package com.qdl.rym.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qdl.rym.base.BaseService;
import com.qdl.rym.base.ResultModel;
import com.qdl.rym.exception.BusinessException;
import com.qdl.rym.system.dto.AlbumDTO;
import com.qdl.rym.system.mapper.AlbumFileMapper;
import com.qdl.rym.system.mapper.AlbumMapper;
import com.qdl.rym.system.mapper.FileRecordMapper;
import com.qdl.rym.system.po.Album;
import com.qdl.rym.system.po.AlbumFile;
import com.qdl.rym.system.po.FileRecord;
import com.qdl.rym.system.service.AlbumService;
import com.qdl.rym.util.FileUtil;
import com.qdl.rym.util.RegexUtil;
import com.qdl.rym.util.config.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: AlbumServiceImpl
 * @Author: ChenXueLi
 * @Date: 2019/3/19 14:15
 * 你是一段成熟的代码，要学会自己改bug
 */
@Service
public class AlbumServiceImpl extends BaseService<Album> implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private FileRecordMapper fileRecordMapper;
    @Autowired
    private AlbumFileMapper albumFileMapper;

    /**
     * 上传信息
     * @param album       其他信息
     * @param resultModel 返回信息
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAlbum(Album album, List<Integer> fileIdList, ResultModel resultModel) throws Exception {
        int effectRows = albumMapper.addAlbum(album);
        if(effectRows != 1) {
            throw new BusinessException(-102, "保存文件错误");
        }
        List<AlbumFile> list = new ArrayList<>();
        for(Integer i : fileIdList) {
            AlbumFile albumFile = new AlbumFile();
            albumFile.setFkFileId(i);
            albumFile.setFkAlbumId(album.getAlbumId());
            list.add(albumFile);
        }

        albumFileMapper.batchAddAlbumFile(list);

        resultModel.setCode(0);
        resultModel.setData(null);
        resultModel.setMessage("保存成功");
    }

    /**
     * 修改信息
     * @param album       其他信息
     * @param resultModel 返回信息
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAlbum(Album album, List<Integer> fileIdList, ResultModel resultModel) throws Exception {
        AlbumDTO albumDTO = albumMapper.getAlbumDTOById(album.getAlbumId());
        if(albumDTO == null) {
            throw new BusinessException(-101, "未查询到指定的信息");
        }
        Album albumGet = albumDTO.getAlbum();
        albumGet.setModifyTime(new Date());

        albumFileMapper.deleteAlbumFile(album.getAlbumId());

        List<AlbumFile> list = new ArrayList<>();
        for(Integer i : fileIdList) {
            AlbumFile albumFile = new AlbumFile();
            albumFile.setFkFileId(i);
            albumFile.setFkAlbumId(album.getAlbumId());
            list.add(albumFile);
        }

        albumFileMapper.batchAddAlbumFile(list);

        if(RegexUtil.isNotNull(album.getTitle())) {
            albumGet.setTitle(album.getTitle());
        }
        if(RegexUtil.isNotNull(album.getDescribe())) {
            albumGet.setDescribe(album.getDescribe());
        }

        int effectRows = albumMapper.updateAlbum(albumGet);
        if(effectRows != 1) {
            throw new BusinessException(-102, "保存文件错误");
        }
        resultModel.setCode(0);
        resultModel.setData(null);
        resultModel.setMessage("保存成功");
    }

    /**
     * 删除信息
     * @param albumId
     * @param resultModel
     * @throws Exception
     */
    @Override
    public void deleteAlbum(Integer albumId, ResultModel resultModel) throws Exception {
        AlbumDTO albumDTO = albumMapper.getAlbumDTOById(albumId);
        if(albumDTO == null) {
            throw new BusinessException(-101, "未查询到指定的信息");
        }
        albumDTO.getAlbum().setModifyTime(new Date());

        int effectRows = albumMapper.deleteAlbum(albumDTO.getAlbum());
        if(effectRows != 1) {
            throw new BusinessException(-102, "删除文件错误");
        }
        resultModel.setCode(0);
        resultModel.setData(null);
        resultModel.setMessage("删除成功");
    }

    /**
     * 根据id查询信息
     * @param albumId
     * @param resultModel
     * @throws Exception
     */
    @Override
    public void getAlbumById(Integer albumId, ResultModel resultModel) throws Exception {
        AlbumDTO albumDTO = albumMapper.getAlbumDTOById(albumId);
        if(albumDTO == null) {
            throw new BusinessException(-101, "未查询到指定的信息");
        }
        resultModel.setCode(0);
        resultModel.setData(albumDTO);
        resultModel.setMessage("查询成功");
    }

    /**
     * 查询列表信息（分页）
     * @param resultModel
     * @throws Exception
     */
    @Override
    public void getAlbums(ResultModel resultModel) throws Exception {
        PageHelper.startPage(resultModel.getPage(), resultModel.getRows());
        List<AlbumDTO> albumDTOList = albumMapper.getAlbums(resultModel.getSearchMap());
        PageInfo<AlbumDTO> pageInfo = new PageInfo<>(albumDTOList);

        if(albumDTOList.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for(AlbumDTO a : albumDTOList) {
                idList.add(a.getAlbumId());
            }
            List<AlbumDTO> newList = albumMapper.getAlbumsByIdList(idList);
            pageInfo.setList(newList);
        }

        resultModel.setCode(0);
        resultModel.setData(pageInfo);
        resultModel.setMessage("查询成功");
    }

    /**
     * 上传文件
     * @param file
     * @param resultModel
     * @throws Exception
     */
    @Override
    public void uploadImg(MultipartFile file, ResultModel resultModel) throws Exception {
        UploadFile uploadFile = new UploadFile(file);
        uploadFile.randomName();
        // 判断是否是图片资源

        // 写入文件到本地
        FileRecord fileRecord = FileUtil.getInstance().save(uploadFile, "/upload/file");
        int effectRows = fileRecordMapper.insert(fileRecord);
        resultModel.setCode(0);
        resultModel.setData(fileRecord);
        resultModel.setMessage("上传成功");
    }
}
